package com.cmd.user;

import com.alibaba.fastjson.JSONObject;
import com.mgr.DriveLineMgr;
import com.mgr.DriverOrderMgr;
import com.player.CommonUser;
import com.player.DriverUser;
import com.player.User;
import com.player.WorldMgr;
import com.qy.bean.DriveLine;
import com.qy.bean.OrderDriver;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 车主查看坐标
 * @author zwl
 * 2018年2月3日
 */
public class PointCheckCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		JSONObject reJs = new JSONObject();
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("serverOrderId")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		int serverOrderId = param.getIntValue("serverOrderId");
		OrderDriver od = DriverOrderMgr.getDriverOrder(serverOrderId);
		if(od==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist.getType());
			user.sendPacket(reJs);
			return;
		}
		/*
		 * 如果订单完成则显示路线图
		 * 
		 */
		if(od.getStatus()==StaticData.DriverOrderStatus.END.getType()){
		    DriveLine driveLine =  DriveLineMgr.getDriveLine(od.getId());
		    if(driveLine==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist2.getType());
			user.sendPacket(reJs);
			return;
		    }
		    reJs.put("pointList", DriveLineMgr.turnToList(driveLine));
		   // 如果订单未完成显示司机当前坐标
		}else{
		    if(!WorldMgr.isExist(od.getDriverId(), StaticData.userType.DRIVER_USER.getType())){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist2.getType());
			user.sendPacket(reJs);
			return;
		    }
			DriverUser dUser = (DriverUser) WorldMgr.getGamePlayer(od.getDriverId(), 
				StaticData.userType.DRIVER_USER.getType());
			reJs.put("point", dUser.getMyPoint());
			/*
			 * 设置车主同步开关
			 */
			CommonUser cUser = (CommonUser) user;
			cUser.setSyncServerId(od.getId());
		}
		
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		user.sendPacket(reJs);
		return;
	}

}
